package com.addplus.townmall.server.scheduled.scheduletask;

import com.addplus.townmall.server.api.mapper.town.*;
import com.addplus.townmall.server.api.model.town.ShopCommentRecord;
import com.addplus.townmall.server.api.model.town.ShopParticipateActive;
import com.addplus.townmall.server.api.model.town.ShopProductDescribe;
import com.addplus.townmall.server.api.model.town.SpikeActive;
import com.addplus.townmall.server.api.utils.date.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 类名: UpdateCommentTask
 *
 * @author zhangjiehang
 * @version V1.0
 * @date 2018/11/8 2:57 PM
 * @description 类描述: 统计商品评分定时器
 */
@Component
public class UpdateCommentTask {

    private Logger logger = LoggerFactory.getLogger(UpdateCommentTask.class);

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    private SpikeActiveMapper spikeActiveMapper;

    @Autowired
    private ShopParticipateActiveMapper shopParticipateActiveMapper;

    @Autowired
    OrderItemsFightMapper orderItemsFightMapper;

    @Autowired
    TownOrderFightMapper townOrderFightMapper;
    @Autowired
    MemberIntegralMapper memberIntegralMapper;
    @Autowired
    ShopCommentRecordMapper shopCommentRecordMapper;
    @Autowired
    ShopProductDescribeMapper shopProductDescribeMapper;

    /**
     * 方法描述：统计商品评分
     *
     * @throws Exception
     * @author zhangjiehang
     * @date 2018/11/8 3:00 PM
     */
    @Scheduled(cron = "0 0 0 * * *")
    private void updateComment() throws Exception {
        logger.info("统计商品评分定时任务开始");
        List<ShopCommentRecord> shopCommentRecords = shopCommentRecordMapper.selectTodayRecord();
        if (shopCommentRecords == null || shopCommentRecords.isEmpty()) {
            logger.info("统计商品评分定时任务，列表未空");
            return;
        } else {
            //根据商品id分组
            Map<Integer, List<ShopCommentRecord>> map = shopCommentRecords.stream().filter(o -> Objects.nonNull(o.getShopProductId())).collect(Collectors.groupingBy(ShopCommentRecord::getShopProductId));
            for (Map.Entry<Integer, List<ShopCommentRecord>> entry : map.entrySet()) {
                Integer shopScore=0;
                Integer shopServie=0;
                Integer shopLogistics=0;
                List<ShopCommentRecord> shopCommentRecordList=entry.getValue();
                for (ShopCommentRecord shopCommentRecord:shopCommentRecordList){
                    shopScore=shopScore+shopCommentRecord.getShopScore();
                    shopServie=shopServie+shopCommentRecord.getShopServie();
                    shopLogistics=shopLogistics+shopCommentRecord.getShopLogistics();
                }
                QueryWrapper<ShopProductDescribe> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("shop_product_id",shopCommentRecordList.get(0).getShopProductId());
                queryWrapper.eq("is_deleted",0);
                ShopProductDescribe shopProductDescribe=shopProductDescribeMapper.selectOne(queryWrapper);
                if(shopProductDescribe==null){
                    return ;
                }else{
                    if(shopProductDescribe.getShopProductRate()==null){
                        shopProductDescribe.setShopProductRate(new BigDecimal(0));
                    }
                    shopProductDescribe.setShopProductRate(new BigDecimal(shopScore).add(shopProductDescribe.getShopProductRate()));
                    if(shopProductDescribe.getShopProductServiceRate()==null){
                        shopProductDescribe.setShopProductServiceRate(new BigDecimal(0));
                    }
                    shopProductDescribe.setShopProductServiceRate(new BigDecimal(shopServie).add(shopProductDescribe.getShopProductServiceRate()));
                    if(shopProductDescribe.getShopProductLogisticsRate()==null){
                        shopProductDescribe.setShopProductLogisticsRate(new BigDecimal(0));
                    }
                    shopProductDescribe.setShopProductLogisticsRate(new BigDecimal(shopLogistics).add(shopProductDescribe.getShopProductLogisticsRate()));
                    if(shopProductDescribe.getShopProductOverallRate()==null){
                        shopProductDescribe.setShopProductOverallRate(new BigDecimal(0));
                    }
                    shopProductDescribe.setShopProductOverallRate(shopProductDescribe.getShopProductOverallRate().add(shopProductDescribe.getShopProductRate().add(shopProductDescribe.getShopProductServiceRate()).add(shopProductDescribe.getShopProductLogisticsRate()).divide(new BigDecimal(3),2)));
                    shopProductDescribe.setGmtModified(new Date());
                    int count=shopProductDescribeMapper.updateById(shopProductDescribe);
                    if(count<1){
                        return ;
                    }
                }
            }
        }
        logger.info("统计商品评分定时任务完成");
    }

}
